import {
  fetchHome
} from '../../services/home/home';
import {
  fetchGoodsList
} from '../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';
import {
  getEnsureList,
  getSwiper
} from '../../api/index'


Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    img: [],
    accessToken: '',
    ensureList: [],
    kefuSrc: '',
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();

  },

  init() {
    this.loadHomePage();
    this.getEnsureListFn()
    this.getSwiperFn()


  },
  goInfo(e) {
    const id = e.currentTarget.dataset.id;
    console.log(id, 'idid')
    wx.navigateTo({
      url: '/pages/cart/itemInfo/index?id=' + id,
    });
  },
  getSwiperFn() {
    getSwiper({
      id: 30
    }).then((res) => {
      console.log(res.data.item, 'res');
      if (res.data.item && res.data.item.length > 0) {
        const urls = res.data.item.map(v => "https://oss.peibanmao.com/" + v.url);
        this.setData({
          kefuSrc: urls[0],
        });
        console.log(this.data.kefuSrc, ' this.data.kefuSrc')
      }
    })
  },
  async getEnsureListFn() {
    const res = await getEnsureList()
    let item = res.data.item || []
    console.log(item, 'itemitem')
    this.setData({
      ensureList: item,
    })
  },
  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({
      swiper,
      tabList
    }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const nextList = await fetchGoodsList(pageIndex, pageSize);
      this.setData({
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  goodListClickHandle(e) {
    const {
      index
    } = e.detail;
    const {
      spuId
    } = this.data.goodsList[index];
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}`,
    });
  },

  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  navToSearchPage() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    });
  },

  // navToActivityDetail({ detail }) {
  //   const { index: promotionID = 0 } = detail || {};
  //   wx.navigateTo({
  //     url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
  //   });
  // },
});